package b2.scene.fight
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.fight.skills.BasicSkill;
	import b2.scene.fight.skills.SkillHelper;
	import b2.util.*;
	import b2.widget.*;
	
	import flash.filters.*;
	
	import org.flixel.*;

	public class BulletSprite extends FlxSprite
	{
		//子弹速度
		public static const BULLET_VELOCITY:uint = 240;
		//子弹大小
		public static const BULLET_WIDTH:uint = 14;
		//子弹图片大小
		public static const BULLET_IMG_WIDTH:uint = 45;
		//子弹头前方位置
		public static const FRONT_POSX:Array = [BULLET_WIDTH / 2, BULLET_WIDTH + 1, BULLET_WIDTH / 2, -1];  
		public static const FRONT_POSY:Array = [-1, BULLET_WIDTH /2, BULLET_WIDTH + 1, BULLET_WIDTH / 2];
		//子弹唯一号
		public var bulletId:int;
		//所属的坦克
		public var tankId:int;
		//子弹的类型
		public var bulletIndex:int;
		//势力阵营（0, 上方玩家; 1, 下方玩家; -1, 电脑势力; -2, 总部)
		public var side:int;
		//攻击伤害
		public var attackPoint:int;
		//被动移动（不主动移动，只接收来自服务器端的移动信息）
		public var passiveMove:Boolean;
		//是否是暴击
		public var criticalFlag:Boolean;
		//效果索引
		public var effectIndex:int;
		//效果值
		public var effectValue:int;
		//效果持续时间
		public var effectDuration:int;
		//方向
		private var _direction:int;
		//技能
		private var boomSkill:BasicSkill;
		
		public function BulletSprite(bulletId:int, bulletIndex:int, tankId:int, side:int, attackPoint:int, criticalFlag:Boolean, 
			effectIndex:int = -1, effectValue:int = -1, effectDuration:int = -1)
		{
			super();
			this.bulletId = bulletId;
			this.bulletIndex = bulletIndex;
			this.tankId = tankId;
			this.side = side;
			this.attackPoint = attackPoint;
			this.criticalFlag = criticalFlag;
			this.effectIndex = effectIndex;
			this.effectValue = effectValue;
			this.effectDuration = effectDuration;
			this.passiveMove = true;
			var bulletConfig:BulletConfig = Config.bulletConfigs[bulletIndex];
			var BulletImage:Class = ResourceHelper.getInstance().getClassByName(bulletConfig.bulletImage);
			ExternalImage.setData(BulletImage, bulletConfig.bulletImage, 135, 135, bulletConfig.bulletFilter);
			loadGraphic(ExternalImage, true, true, BULLET_IMG_WIDTH);
			this.offset.x = (BULLET_IMG_WIDTH - BULLET_WIDTH) / 2;
			this.offset.y = (BULLET_IMG_WIDTH - BULLET_WIDTH) / 2;
			
			this.width = BULLET_WIDTH;
			this.height = BULLET_WIDTH;

			this.addAnimation("right", [0]);
			this.addAnimation("down", [1]);
			this.addAnimation("up", [2]);
			var playerState:PlayState = FlxG.state as PlayState;
			var tank:TankSprite = playerState.tanks[tankId];
			if (tank.specialBullet != null)
				boomSkill = SkillHelper.createSkill(this.tankId, tank.side, tank.specialBullet.bulletSkillIndex, tank.level, tank.specialBullet);
		}
		
		public function set direction(d:int):void
		{
			_direction = d % 4;
			if (d == 0)
			{
				this.facing = FlxObject.UP;
				this.play("up");
			}
			else if (d == 1)
			{
				this.play("left");
				this.facing = FlxObject.RIGHT;
			}
			else if (d == 2)
			{
				this.facing = FlxObject.DOWN;
				this.play("down");
			}
			else
			{
				this.play("left");
				this.facing = FlxObject.LEFT;
			}
		}
		
		public function get direction():int
		{
			return _direction;
		}
		
		public function getFrontPoint():FlxPoint
		{
			return new FlxPoint(this.x + FRONT_POSX[this.direction], this.y + FRONT_POSY[this.direction]);
		}
		
		public override function kill():void
		{
			//如果子弹有技能
			if (boomSkill)
				boomSkill.discharge(this.x + FRONT_POSX[direction], this.y + FRONT_POSY[direction], this.direction);
			super.kill();
			var bulletConfig:BulletConfig = Config.bulletConfigs[bulletIndex];
			var BulletExplode:Class = ResourceHelper.getInstance().getClassByName(bulletConfig.explodeImage);
			var explodeMc:BasicMc = new BasicMc(BulletExplode, 40, false, true);
			if (bulletConfig.explodeFilter != null)
				explodeMc.filters = [new ColorMatrixFilter(bulletConfig.explodeFilter)];
			if (bulletConfig.explodeWidth > 0)
			{
				explodeMc.width = bulletConfig.explodeWidth;
				explodeMc.scaleY = explodeMc.scaleX;
			}
			explodeMc.x = this.x + FRONT_POSX[direction];
			explodeMc.y = this.y + FRONT_POSY[direction];
			FightScene.getInstance().effectLayer.addChild(explodeMc);
	
		} 
		
	}
}